//
//  JSSWelcomViewController.swift
//  WeiBoItem
//
//  Created by jss on 16/4/20.
//  Copyright © 2016年 jss. All rights reserved.
//

import UIKit
import SDWebImage

class JSSWelcomViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor(white: 247/255.0, alpha: 1)
        
        setupUI()
        
    }
    
    
    //设置子控件
    private func setupUI() {
        
        //添加头像
        self.view.addSubview(iconImageView)
        //添加lable
        self.view.addSubview(messegeLable)
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self.view)
            make.top.equalTo(200)
            make.width.height.equalTo(80)
        }
        
        messegeLable.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(iconImageView)
            make.top.equalTo(iconImageView.snp_bottom).offset(15)
        }
      
    }
    
    //怎样实现弹簧动画
    //在什么时候执行动画
    //界面没展示出来是不需要执行动画的 所以在view一显示就马上执行动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        //更新约束
        iconImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(100)
        }
        
        //执行弹簧动画
        /**
        1. 持续时间
        2. 延时多少秒之后执行
        3. Damping -> 阻尼 0-1，取值越小，幅度越大
        4. Velocity -> 初始化速度
        5. 动画的选项
        6. 动画执行完成之后调用的闭包
        */
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            //重新布局
            self.view.layoutIfNeeded()
            
            
            }) { (_) -> Void in
                
                UIView.animateWithDuration(1, animations: { () -> Void in
                    
                    self.messegeLable.alpha = 1
                    
                    }, completion: { (_) -> Void in
                        
                        //执行动画以后回调的方法  执行动画后跳转控制器
                        print("动画执行完成 切换控制器")
                        
                        
                        //关于app应用程序的相关信息最好写在appdelegate里 这里用通知 实现在delegate里面切换控制器
                        //let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate
                        
                        //登录成功跳转到 MainTabBarController(主页面)
                        //appDelegate?.window?.rootViewController = MainTabBarController()
                        
                        //发送通知 切换控制器
                        NSNotificationCenter.defaultCenter().postNotificationName(JSSChangeRootVC, object:nil, userInfo: nil)
                })
        }
    }
    
  
    
    
    
    //懒加载 头像
    lazy var iconImageView:UIImageView = {
        
    let imageView = UIImageView(image: UIImage(named: "avatar_default_small"))
        
        imageView.layer.cornerRadius = 40
        
        imageView.layer.masksToBounds = true
        
        
        if let avatarImage = JSSUserAccountModel.sharedAccontModel.account?.avatar_large {
            
            imageView.sd_setImageWithURL(NSURL(string: avatarImage), placeholderImage: UIImage(named: "avatar_default_small"))
            
        }
        
        return imageView
    
    }()
    
    //懒加载lable
    lazy var messegeLable:UILabel = {
        
        let lable = UILabel(textColor: UIColor.darkGrayColor(), textFont: 15)
        
        lable.text = "欢迎回来"
        
         lable.alpha = 0
        
        return lable
    
    }()
    
  
    
}
